<?php
session_start();
    $dbhost='localhost'; //country 0
    $dbuser='s91199'; //username 0
    $dbpass='123456'; //password 0
    $dbname='s91199'; //name 0
    /*
    $host='localhost'; //country 0
    $username='root'; //username 0
    $password=''; //password 0
    $dbname='mydb'; //name 0*/
$conn=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$username = mysqli_real_escape_string($conn,$_POST['user']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$password = sha1($password);
$role=mysqli_real_escape_string($conn,$_POST['role']);
if ($role=="school") {
    $sql = "SELECT * FROM contact_school cs,school s WHERE cs.contact_email='$username' AND cs.contact_password ='$password' AND s.school_status ='active' AND cs.school_id=s.school_id";
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    $count = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    if ($count==1) {
        $_SESSION['school_id']=$row['contact_school_id'];
        $_SESSION['user_name']=$row['contact_name'];
        $_SESSION['contact_salutation']=$row['contact_salutation'];
        $_SESSION['role']=$_POST['role'];
        $_SESSION['message']="Congratulation";
        if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie('username', $_POST['user'], time()+60*60*24*365);
            setcookie('password', md5($_POST['password']), time()+60*60*24*365);
        }else {
            /* Cookie expires when browser closes */
            setcookie('username', $_POST['user'], false);
            setcookie('password', md5($_POST['password']), false);
        }
        header('Location: ' . $_SERVER['HTTP_REFERER']);
    }else {
        $_SESSION['message']="Invalid username or password.";
        header("Location: ../include/home/homepage.php");
    }
}else if ($role=="volunteer") {
    $sql = "SELECT * FROM volunteer WHERE volunteer_email='$username' AND volunteer_password ='$password'";
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    $count = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    if ($count==1) {
        $_SESSION['volunteer_id']=$row['volunteer_id'];
        $_SESSION['user_name']=$row['volunteer_firstname'];
        $_SESSION['user_name2']=$row['volunteer_surname'];
        $_SESSION['volunteer_salutation']=$row['volunteer_salutation'];
        $_SESSION['role']=$_POST['role'];
        $_SESSION['message']="Congratulation";
        if (isset($_POST['rememberme'])) {
            /* Set cookie to last 1 year */
            setcookie('username', $_POST['user'], time()+60*60*24*365);
            setcookie('password', md5($_POST['password']), time()+60*60*24*365);
        }else {
            /* Cookie expires when browser closes */
            setcookie('username', $_POST['user'], false);
            setcookie('password', md5($_POST['password']), false);
        }
        header('Location: ' . $_SERVER['HTTP_REFERER']);
    }  else {
        $_SESSION['message']="Invalid";
        header("Location: ../include/home/homepage.php");
    }

}

?> 